package utils;



import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendMail {
    //邮件发送的服务器
//    private static String host = "smtp.163.com";

    public static void sendMail(String host, String sender, String pws, String[] recipient, String topic, String content) {
        try {
            Properties properties = new Properties();
            properties.setProperty("mail.host", host);
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");
            //QQ存在一个特性设置SSL加密
//            MailSSLSocketFactory sf = null;
//            try {
//                sf = new MailSSLSocketFactory();
//            } catch (GeneralSecurityException e) {
//                e.printStackTrace();
//            }
//            sf.setTrustAllHosts(true);
//            properties.put("mail.smtp.ssl.enable", "true");
//            properties.put("mail.smtp.ssl.socketFactory", sf);
            //创建一个session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(sender, pws);
                }
            });
            //开启debug模式
            session.setDebug(true);
            //获取连接对象
            Transport transport = null;
            try {
                transport = session.getTransport();
                if (transport == null) {
                    throw new RuntimeException("获取连接对象失败");
                }
            } catch (NoSuchProviderException e) {
                throw new RuntimeException("获取连接对象失败",e);
            }
            //连接服务器
            transport.connect(host, sender, pws);
            //创建一个邮件发送对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //邮件发送人
            mimeMessage.setFrom(new InternetAddress(sender));
            //邮件接收人
            Address[] addresses = getRecipientAddresses(recipient);
            mimeMessage.setRecipients(Message.RecipientType.TO, addresses);
            //邮件标题
            mimeMessage.setSubject(topic);
            //邮件内容
            mimeMessage.setContent(content, "text/html;charset=UTF-8");
            //发送邮件
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println(ExceptionUtils.toString(e));
            throw new RuntimeException("邮件发送失败", e);
        }

    }

    /**
     * @param recpient
     * @return
     * @throws Exception
     */
    public static Address[] getRecipientAddresses(String[] recpient) throws Exception {

        Address[] address = new Address[recpient.length];
        for (int i = 0; i < recpient.length; i++) {
            address[i] = new InternetAddress(recpient[i]);
        }

        return address;
    }

}



